<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: GetCircle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\note;

use app\api\model\dao\Note;
use app\api\model\dao\NotePraise;
use app\api\model\dao\NoteReply;
use app\api\model\data\note\NoteData;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;

class GetNoteList extends PageBase
{

    private $objNote;
    private $objNoteReply;
    private $objNoteData;
    private $objNotePraise;

    public function __construct()
    {
        parent::__construct();

        $this->objNote = new Note();
        $this->objNoteReply = new NoteReply();
        $this->objNoteData = new NoteData();
        $this->objNotePraise = new NotePraise();
    }

    /**
     * page 入口
     *
     * @param $param
     * @return array
     * @throws Exception
     */
    public function excute($param)
    {
        $this->checkToken($param);

        $limit = isset($param['limit']) ? $param['limit'] : 10;
        $offset = (isset($param['offset']) && ($param['offset'] >= 0)) ? $param['offset'] : 0;

        $this->user_id = !empty($this->user_id) ? $this->user_id : 0;

        $cir_id = isset($param['cir_id']) && !empty($param['cir_id']) ? $param['cir_id'] : 0;

        $this->data['list'] = [];

        if (isset($param['tab']) && !empty($param['tab'])) {
            switch ($param['tab']) {
                case Flag::CONST_NOTE_TAB_EXQUISITE:
                    $arrNote = $this->objNoteData->exquisiteNote($cir_id, $offset, $limit);
                    break;
                case Flag::CONST_NOTE_TAB_HOT:
                    $arrNote = $this->objNoteData->hotNote($cir_id, $offset, $limit);
                    break;
                case Flag::CONST_NOTE_TAB_ORIGINAL:
                    $arrNote = $this->objNoteData->originalNote($cir_id, $offset, $limit);
                    break;
                default :
                    $arrNote = $this->objNoteData->newNote($cir_id, $offset, $limit);
            }
        } else {
            $arrNote = $this->objNoteData->newNote($cir_id, $offset, $limit);
        }

        foreach ($arrNote as &$note) {
            $note['is_praise'] = 0;
            $conds = [];
            $conds['note_id'] = ['=', $note['note_id']];
            $conds['userid'] = ['=', $this->user_id];
            $retCnt = $this->objNotePraise->getNotePraiseCountByConds($conds);
            if (!empty($retCnt)) {
                $note['is_praise'] = 1;
            }
        }
        unset($note);

        if (!empty($noteids)) {
            $this->objNotePraise->getNotePraise();
        }


        $this->data['list'] = $arrNote;
        return $this->getRes();
    }
}
